/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package InteracciónBD;

import java.io.*;
import java.sql.*;
import java.util.HashMap;

/**
 * Clase que contiene un conjuno de Access para ingresar a las BBDD's.
 * @author Felipe Rachid - Javier Pérez
 */
public class DDBBAccess
{
    private ResultSet resultSet;
    private HashMap<Integer, Access> table;
    
    /**********************************************************************/
    /**********************************************************************/
    /**********************************************************************/

    /**
     * @return resultSet Tabla de resultados generados por el Statement.
     */
    private ResultSet getResultSet()
    { return resultSet; }

    /**
     * @param resultSet Tabla de resultados generados por el Statement.
     */
    private void setResultSet(ResultSet resultSet)
    { this.resultSet = resultSet; }
    
    /**
     * @return table HashMap que contine los Access de las BBDD's
     * y un campo String con su nombre.
     */
    private HashMap<Integer, Access> getTable()
    { return table; }

    /**
     * @param table HashMap que contine los Access de las BBDD's
     * y un campo String con su nombre.
     */
    private void setTable(HashMap<Integer, Access> table)
    { this.table = table; }
    
    /**********************************************************************/
    /**********************************************************************/
    /**********************************************************************/
    
    /**
     * Constructor de la clase DDBBAccess que incializa un HashMap con un 
     * conjunto de campos String y un Access asociada a cada String.
     */
    public DDBBAccess()
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Introduce the location of your SQL DB access table ");
            System.out.print("(For example: jdbc:mysql://localhost/access_meaning): ");
            String location = br.readLine();
            System.out.print("Introduce your user name ");
            System.out.print("(For example: root): ");
            String user = br.readLine();
            System.out.print("Introduce your password: ");
            String pass = br.readLine();
            //Se crea la conexión.
            Conection myConection = new Conection();
            //Se abre la conexión con los datos especificos del diccionario.
            myConection.openConnection(location, user, pass);
            //Se capturan todos los datos.
            setResultSet(myConection.obtainResultSet("select * from access"));
            //Se Ccea el HashMap.
            this.setTable(new HashMap<Integer, Access>());
            //Se cargan los datos de acceso de las BBDD's al HashMap.
            while (this.getResultSet().next())
            {
                Access myAccess = new Access(getResultSet().getInt("idaccess"),
                    getResultSet().getString("Name"), getResultSet().getString("Location"), 
                    getResultSet().getString("User"), getResultSet().getString("Pass"));
                this.getTable().put(myAccess.getId(), myAccess);
            }
            //Se cierra la conexión.
            myConection.closeConcection();
        }
        catch (Exception e) {System.err.println(e.toString());}
    }
    
    public int accessNumber()
    { return this.getTable().size(); }
    
    public Access obtainAccess(Integer i)
    { return this.getTable().get(i); }
}